package com.jiangore.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Todo
 * @Author jiangtao
 * @CreateBy 2020-03-14 7:19 PM
 * @UpdateBy jiangtao
 * @Version v1.0
 */
@Slf4j
@RestController
@RequestMapping
public class HomeController {

    @RequestMapping("set")
    public String set(@RequestParam(name = "key", required = true, defaultValue = "key") String key,
                       @RequestParam(name = "value", required = true, defaultValue = "value") String value,
                       HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute(key, value);
        log.debug("设置session, sessionId: %s; key: %s; value: %s",session.getId(), key, value);
        return "sessionId:" + session.getId();
    }

    @RequestMapping("get/{key}")
    public String get(@PathVariable("key") String key, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String value = (String) session.getAttribute(key);
        log.debug("获取session, sessionId: %s; %s = %s", key, value);
        return "sessionId:" + session.getId() + "; " + key + ":" + value;
    }
}
